package com.beerandjesus.photogallery.image;

import com.beerandjesus.common.io.ImageFilenameFilter;
import com.beerandjesus.common.io.TokenizedFile;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Creates a list of PhotoGalleryImage objects from a directory
 * containing photos and thumbnails created by CreateManyThumbnails.
 * <p/>
 * The filesystem directory is obtained by examining the
 * HttpServletRequest and combining the request path with this
 * object's baseDir.
 *
 * @see com.beerandjesus.photogallery.thumbnail.CreateManyThumbnails
 */
public class PhotoGalleryImageListImpl implements PhotoGalleryImageList {
    private String baseDir;

    public PhotoGalleryImageListImpl(String baseDir) {
        this.baseDir = baseDir;
    }

    @SuppressWarnings("unchecked")
    public List<PhotoGalleryImage> getList(HttpServletRequest request) {
        String uri = request.getRequestURI();
        String dirPath;
        // find the directory path
        if (uri.endsWith(".jsp") || uri.endsWith("/")) {
            int lastSlash = uri.lastIndexOf("/");
            dirPath = uri.substring(0, lastSlash);
        } else {
            dirPath = uri;
        }
        File fullPath = new File(baseDir + uri);
        if (!fullPath.isDirectory()) {
            fullPath = fullPath.getParentFile();
        }
        String[] fileStrings = fullPath.list(new ImageFilenameFilter());
        List<String> fileList = Arrays.asList(fileStrings);

        // Create 2 lists: one with thumbnails, one without
        List<String> thumbList = new ArrayList(CollectionUtils.select(fileList, new Predicate() {
            public boolean evaluate(Object object) {
                String fileName = (String) object;
                TokenizedFile file = new TokenizedFile(new File(fileName));
                String root = file.nameRoot();
                return root.endsWith("_tn");
            }
        }));
        List<String> photoList = new ArrayList(CollectionUtils.disjunction(fileList, thumbList));

        Collections.sort(photoList);
        Collections.sort(thumbList);

        // Construct the image list
        List<PhotoGalleryImage> images = new ArrayList<PhotoGalleryImage>();
        int index = -1;
        for (String photo : photoList) {
            index++;
            String thumb = thumbList.get(index);
            String photoPath = dirPath + "/" + photo;
            String thumbPath = dirPath + "/" + thumb;
            PhotoGalleryImage img = new PhotoGalleryImage();
            img.setPictureUri(photoPath);
            img.setThumbUri(thumbPath);
            images.add(img);
        }
        return images;
    }

    public String getBaseDir() {
        return baseDir;
    }

    public void setBaseDir(String baseDir) {
        this.baseDir = baseDir;
    }
}
